<?php

class EtapasController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Loader::loadClass('Etapas');
        Zend_Loader::loadClass('Tarefas');
        Zend_Loader::loadClass('Categorias');
    }

    public function indexAction()
    {
        $etapas = new Etapas();

        $form = new Application_Form_ListaCategoria();
        $form->submit->setLabel('Pesquisar');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $categoria_id = (int)$form->getValue('categoria_id');
                $where=null;
                $aplicacao = $form->getValue('aplicacao');
                if($aplicacao!=null) $where = "(aplicacao='Comum' or aplicacao='".$aplicacao."')";
                if($categoria_id>0){
                    ($where!=null) ? $where .= 'and (categoria_id='.$categoria_id.')'
                    : $where='(categoria_id='.$categoria_id.')';
                    Zend_Registry::set('filtro',$where);
                    $titulo = '-- '.$aplicacao.' --';
                    Zend_Registry::set('titulo_etapas',$titulo);
                    $this->view->etapas = $etapas->getCustomSelect($where,'ordem');
                }
                else
                {
                    $titulo = '-- Todos os tipos de cursos --';
                    Zend_Registry::set('titulo_etapas',$titulo);
                    Zend_Registry::set('filtro',$where);
                    $this->view->etapas = $etapas->getCustomSelect($where,'ordem');

                }

            } else {
                $form->populate($formData);
            }
        } else
        {
            $this->view->etapas = $etapas->getCustomSelect(null,'ordem');
        }

    }

    public function addAction()
    {
        $form = new Application_Form_Etapa();
        $form->submit->setLabel('Adicionar');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $aplicacao = $form->getValue('aplicacao');
                $categoria_id = $form->getValue('categoria_id');
                $etapa = $form->getValue('etapa');
                $ordem = $form->getValue('ordem');
                $etapas = new Etapas();
                $etapas->addEtapa($aplicacao,$categoria_id, $etapa, $ordem);
                $this->organizar();
                $this->_redirect('index');
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction()
    {
        $form = new Application_Form_Etapa();
        $form->submit->setLabel('Salvar');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id');
                $aplicacao = $form->getValue('aplicacao');
                $categoria_id = $form->getValue('categoria_id');
                $etapa = $form->getValue('etapa');
                $ordem = $form->getValue('ordem');
                $etapas = new Etapas();
                $ordem_atual = $etapas->find($id)->current()->ordem;
                $etapas->updateEtapa($id, $aplicacao,$categoria_id, $etapa, $ordem);
                if($ordem!=$ordem_atual)
                    $this->organizar();
                $this->_redirect('index');
            } else {
                $form->populate($formData);
            }
        } else
        {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $etapas = new Etapas();
                $form->populate($etapas->getEtapa($id));
            }
        }
    }

    public function deleteAction()
    {
        if ($this->getRequest()->isPost())
        {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Sim') {
                $id = $this->getRequest()->getPost('id');
                $etapas = new Etapas();
                $etapas->deleteEtapa($id);
                $this->organizar();
            }
            $this->_redirect('index');
        } else {
            $id = $this->_getParam('id', 0);
            $etapas = new Etapas();
            $this->view->etapa = $etapas->getEtapa($id);
        }
    }

    public function tarefasAction()
    {
        if (Zend_Registry::isRegistered('filtro')) {
            $where = Zend_Registry::get('filtro');
            $titulo = Zend_Registry::get('titulo_etapas');
        }
        else {
            $where = null;
            $titulo = '-- Todos os tipos de cursos --';
        }
        $string = '<h3>'.$titulo.'</h3><table class="lista_tarefas"><tr><th>Etapa</th></tr>';
        $cont = 0;
        $etapas = new Etapas();
        $result = $etapas->getCustomSelect($where,'ordem');
        foreach($result as $linha)
        {
            $etapa = $etapas->find($linha->id)->current();
            $cont++;
//            if($cont % 2 == 0)
//                $string .= '<tr class="even">';
//            else
                $string .= '<tr class="odd">';
            $string .= '<td>'.$etapa->etapa.'</td></tr><tr><td><ul>';
            $tarefas = $etapa->findDependentRowset('Tarefas','Tarefas', $etapa->select()->order('ordem'));
            $existe_tarefa = 0;
            foreach($tarefas as $tarefa)
            {
                $string .= '<li>'.$tarefa->tarefa.'</li>';
                $existe_tarefa = 1;
            }
            $string .= '</ul>';
            if($existe_tarefa==1)
                $string .= '<br/>';
            $string .= '</td></tr>';
        }
        $string .= '</table>';
        $this->view->etapas = $string;
    }

    private function organizar()
    {
        $etapas = new Etapas();
        $query = $etapas->select()->order('ordem');
        $results= $etapas->fetchAll($query);
        $cont=0;
        foreach ($results as $value) {
            $cont=$cont+2;
            $value->ordem = $cont;
            $value->save();
        }
    }

}

